Object - Based Programming in Fortran 90

نویسندگان

  • Mark G. Gray
  • Randy M. Roberts
چکیده

ion | users needing timing informationdeal with one stopwatch variable instead of arrays of times and names. If the user needs several stopwatches, he simply creates several distinct variables of type stopwatch. Type safety | compilers can check that stopwatch variables are used as stopwatches in procedures. Encapsulation | implementors can easily create or change procedures that use stopwatches since their components are grouped together. Variables of the user-de ned stopwatch and timer type have state and identity, but lack any intrinsic behavior necessary for object-hood. We add behavior by adding methods to obtain genuine objects. 4.2 Pass arguments to methods Our Fortran 90 user-de ned types are given behavior by adding methods: subroutines and functions which operate on a variable of the user-de ned type passed as the rst parameter. We use the naming conventions of prepending the class name and underscore to the method name to further associate it with the class, and of naming the corresponding user-de ned type parameter self, in keeping with the convention of some well known object-oriented languages. The stopwatch user-de ned type becomes a stopwatch class by adding the following methods: subroutine stopwatch_construct(self) 3computed from the Fortran 90 intrinsic SYSTEM CLOCK, which returns integersCOUNT, COUNT RATE, and COUNT MAX based on the system clock Fortran 90 passes arguments by reference; large user-de ned types can be used as parameters without concern over copy overhead. C++ uses this instead of self. Gray and Roberts: Object-Based Programming in Fortran 90 6 subroutine stopwatch_construct_1(self, n) subroutine stopwatch_split(self, name) function stopwatch_resolution() subroutine stopwatch_report(self, u) subroutine stopwatch_destruct(self) The stopwatch construct and stopwatch construct 1 subroutines prepare a stopwatch variable by allocating its name and timer arrays and initializing that data. The stopwatch destruct subroutine deallocates these arrays. Once de ned and initialized, the stopwatch split routine toggles a named split. The stopwatch resolution function is somewhat di erent from the others; it is a class method instead of an object method. A class method a ects the behavior of the entire class; it takes no self parameter and does not rely on the existence of a class variable to perform its job. The stopwatch resolution returns the resolution of the stopwatch class, which is data common to all stopwatch objects. The stopwatch report subroutine reports the times recorded by the split array to logical unit u. The timer user-de ned type becomes a timer class by adding the following methods: subroutine timer_construct(self) subroutine timer_switch(self) function timer_is_on(self) function timer_time(self) function timer_resolution() subroutine timer_destruct(self) The timer construct subroutine prepares a timer variable by initializing its data. The timer destruct subroutine is not really needed here since no deallocation is necessary, but is included in case a change in the implementation of timer required it. Once de ned and initialized, the timer switch routine toggles a timer. The timer is on function returns the timer's state, and the timer time function returns a timer's elapsed time. The timer resolution function is another class method which returns the resolution of a timer. The behavior given to the stopwatch and timer user-de ned types by these methods make them true classes; variables of their types are true objects. By adding methods to the user-de ned types, we have achieved the following desirable programming objectives: Gray and Roberts: Object-Based Programming in Fortran 90 7 Encapsulation | users of the timer class need know nothing about its user-de ned type's components, nor of its procedure's implementation in order to use it. Similarly the developer of the timer class is free to change its internal attributes and method coding as long as he maintains the promised behavior of the class. The timer class underwent several major changes during the course of its development; because of this encapsulation, its user (the stopwatch class) remained unchanged. The user is freed from having to worry about how the class works, and the programmer is freed from having to worry about how the users uses it. 4.3 Allocate storage for objects Fortran 90 supports static allocation (global variables), automatic allocation (local variables), and heap allocation (using the allocate keyword). Unfortunately Fortran 90 does not automatically provide for the allocation (or deallocation) of user de ned type components through the invocation of user-de ned constructors (or the destructor). We compensate for this by giving each class at least one construct method, which is responsible for any allocation and initialization, and one destruct method, which is responsible for any deallocation. The user is responsible for calling one of the constructor subroutines to properly initialize the class, and calling the destructor routine for deallocation after use. This detail is already taken care of in most object-oriented languages, where construction is an automatic part of variable declaration and destruction is an automatic part of variables leaving scope. The stopwatch \default" constructor, stopwatch construct, merely allocates space for 20 timers in split and 20 character strings in name via a call to the stopwatch construct 1 constructor: subroutine stopwatch_construct(self) implicit none type(stopwatch), intent(inout) :: self call stopwatch_construct_1(self, 20) end subroutine stopwatch_construct 6We use the intent keyword to restrict the use of arguments to procedures. The intent(in) argument attribute prohibits a procedure from modifying the argument's content. This corresponds to the const keyword in C++. Gray and Roberts: Object-Based Programming in Fortran 90 8 The stopwatch construct 1 subroutine allocates the speci ed storage in name and split and then calls each timer's construct subroutine to let the timers initialize themselves: subroutine stopwatch_construct_1(self, n) implicit none type(stopwatch), intent(inout) :: self integer, intent(in) :: n

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Object-oriented Scientific Programming with Fortran 90

Fortran 90 is a modern language that introduces many important new features beneficial for scientific programming. While the array-syntax notation has received the most attention, we have found that many modern softaware development techniques can be supported by this language, including object-oriented concepts. While Fortran 90 is not a full object-oriented language it can directly support ma...

متن کامل

Parallel Object-oriented Design in Fortran for Beam Dynamics Simulations∗

In this paper we describe an object-oriented software design approach, using Fortran 90 (F90) and the Message Passing Interface (MPI), for modeling the transport of intense charged particle beams. The object-oriented approach improves the maintainability, resuability, and extensibility of the software, while the use of explicit message passing provides the freedom necessary to achieve high perf...

متن کامل

CHASM: Static Analysis and Automatic Code Generation for Improved Fortran 90 and C++ Interoperability

The relative simplicity and design of the Fortran 77 language allowed for reasonable interoperability with C and C++. Fortran 90, on the other hand, introduces several new and complex features to the language that severely degrade the ability of a mixed Fortran and C++ development environment. Major new items added to Fortran are userdefined types, pointers, and several new array features. Each...

متن کامل

High-performance language interoperability for scientific computing through Babel

High-performance scientific applications are usually built from software modules written in multiple programming languages. This raises the issue of language interoperability which involves making calls between languages, converting basic types, and bridging disparate programming models. Babel provides a featurerich, extensible, high-performance solution to the language interoperability problem...

متن کامل

Object-oriented design patterns in Fortran 90/95: mazev1, mazev2 and mazev3

This paper discusses the concept, application, and usefulness of software design patterns for scientific programming in Fortran 90/95. An example from the discipline of object-oriented design patterns, that of a game based on navigation through a maze, is used to describe how some important patterns can be implemented in Fortran 90/95 and how the progressive introduction of design patterns can ...

متن کامل

Object-Oriented Implementation of Field Recovery and Error Estimation in FEM

1. INTRODUCTION Computer programs for numerical solution of partial diierential equations are traditionally coded in the FORTRAN programming language. The computational intensive parts of such programs, either they are based on the nite element (FE) method or some other numerical procedure, consist mainly of various vector and matrix operations. FORTRAN has therefore been regarded as the best c...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 1997